package com.balance.anole.utils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NumberUtil {

	private static final BigDecimal tinyMoney = new BigDecimal("0.1");
	private static final DecimalFormat moneyFormat = new DecimalFormat("#0.00");
	private static final Pattern moneyPattern = Pattern.compile("(\\d){1,15}(\\.(\\d){0,2})?");

	public static String formatMoney(BigDecimal money){
		if(isTinyMoney(money)){
			return "0.00";
		}
		return moneyFormat.format(money);
	}

	public static String formatBigDecimal(BigDecimal num, String format){
		DecimalFormat moneyFormat = new DecimalFormat(format);
		return moneyFormat.format(num);
	}

	public static boolean isTinyMoney(BigDecimal money){
		if(money.abs().compareTo(tinyMoney) < 0){
			return true;
		}
		return false;
	}

	public static boolean isValidMoney(String moneyStr){
		Matcher matcher = moneyPattern.matcher(moneyStr);
		return matcher.matches();
	}
}
